//
//  PhotoViewController.m
//  Browser
//
//  Created by Gu, Eric on 11/4/14.
//  Copyright (c) 2014 Gu, Eric. All rights reserved.
//

#import "PhotoViewController.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   CGRect frame = self.view.frame;
   self.btnPrevious = [[UIButton alloc]initWithFrame: CGRectMake(0, 20, 50, 50)];
   [self.btnPrevious setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
   [self.btnPrevious setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
   [self.btnPrevious addTarget:self action:@selector(btnPrevious:) forControlEvents:UIControlEventTouchUpInside];
   
   self.btnForward = [[UIButton alloc]initWithFrame: CGRectMake(frame.size.width-50, 20, 50, 50)];
   [self.btnForward setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
   [self.btnForward setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];
   [self.btnForward addTarget:self action:@selector(btnForward:) forControlEvents:UIControlEventTouchUpInside];
   
   self.btnDismiss = [[UIButton alloc]initWithFrame: CGRectMake(frame.size.width/2 - 50, 20, 100, 50)];
   [self.btnDismiss setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
   [self.btnDismiss setImage:[UIImage imageNamed:@"dismiss.png"] forState:UIControlStateNormal];
   [self.btnDismiss addTarget:self action:@selector(btnDismiss:) forControlEvents:UIControlEventTouchUpInside];
   
    
   self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, frame.size.width, frame.size.height-70)];
    self.scrollView.backgroundColor = [UIColor cyanColor];
    self.scrollView.scrollEnabled = YES;
    
   self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
   self.imgView.contentMode = UIViewContentModeCenter;
   self.imgView.backgroundColor = [UIColor grayColor];
    
    // 缩放手势
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [self.imgView addGestureRecognizer:pinchGestureRecognizer];
    [self.imgView setUserInteractionEnabled:YES];
    [self.imgView setMultipleTouchEnabled:YES];
    self.imgView.contentMode = UIViewContentModeScaleAspectFill;

   
   
   [self.view addSubview:self.btnPrevious];
   [self.view addSubview:self.btnForward];
   [self.view addSubview:self.btnDismiss];
   [self.view addSubview:self.scrollView];
   [self.scrollView addSubview:self.imgView];
   [self setViewRotation];
   [self initData];
   
   // Do any additional setup after loading the view.
}
- (void) initData
{
   dbManger = [DBManager getSharedInstance];
   _imgURLs = [dbManger loadAllImageURLs];
   _currentIndex = 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 处理缩放手势
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    UIView *view = pinchGestureRecognizer.view;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        pinchGestureRecognizer.scale = 1;
    }
}


- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
   [self setViewRotation];
}

- (void) setViewRotation
{
   CGRect frame = self.view.frame;
   CGFloat longSide = frame.size.width > frame.size.height? frame.size.width : frame.size.height;
   CGFloat shortSide = frame.size.width < frame.size.height? frame.size.width : frame.size.height;
   
   UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
   if( UIInterfaceOrientationIsLandscape(orientation))
   {
      self.btnPrevious.frame = CGRectMake(0, 20, 50, 50);
      self.btnDismiss.frame = CGRectMake(longSide/2-50, 20, 100, 50);
      self.btnForward.frame = CGRectMake(longSide - 50, 20, 50, 50);
      self.scrollView.frame = CGRectMake(0, 70, longSide, shortSide - 70);
   }else
   {
      self.btnPrevious.frame = CGRectMake(0, 20, 50, 50);
      self.btnDismiss.frame = CGRectMake(shortSide/2 - 50, 20, 100, 50);
      self.btnForward.frame = CGRectMake(shortSide-50, 20, 50, 50);
      self.scrollView.frame = CGRectMake(0, 70, shortSide, longSide-70);
     
   }
   
}


- (IBAction)btnPrevious:(id)sender
{
   NSUInteger imgURLsCount = [_imgURLs count];
   if (imgURLsCount > 0)
   {
      _currentIndex = (_currentIndex - 1 + imgURLsCount) %imgURLsCount;
      NSString* currentURL = [_imgURLs objectAtIndex:_currentIndex];
//      UIImage *image = [[UIImage alloc]initWithData:[_data objectAtIndex:_currentIndex]];
      NSData *currectImageData = [dbManger loadAllImagesByUrl:currentURL];
      UIImage *image = [[UIImage alloc]initWithData:currectImageData];
       self.scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);
//       NSLog(@"scrollview x:%f y:%f  w:%f  h:%f.", self.scrollView.frame.origin.x, self.scrollView.frame.origin.y,self.scrollView.frame.size.width, self.scrollView.frame.size.height);
      [self.imgView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
       //       self.imgView.center = self.imgView.superview.center;
//       NSLog(@"image width:%f  height:%f.", image.size.width, image.size.height);
      
      [self.imgView setImage:image];
       
   }
   NSLog(@"previous btn press");
}

- (IBAction)btnForward:(id)sender
{
   NSUInteger imgURLsCount = [_imgURLs count];
   if (imgURLsCount > 0)
   {
      _currentIndex = (_currentIndex + 1 + imgURLsCount) %imgURLsCount;
      NSString* currentURL = [_imgURLs objectAtIndex:_currentIndex];
      //      UIImage *image = [[UIImage alloc]initWithData:[_data objectAtIndex:_currentIndex]];
      NSData *currectImageData = [dbManger loadAllImagesByUrl:currentURL];
      UIImage *image = [[UIImage alloc]initWithData:currectImageData];
      self.scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);
      //       NSLog(@"scrollview x:%f y:%f  w:%f  h:%f.", self.scrollView.frame.origin.x, self.scrollView.frame.origin.y,self.scrollView.frame.size.width, self.scrollView.frame.size.height);
      [self.imgView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
      //       self.imgView.center = self.imgView.superview.center;
      //       NSLog(@"image width:%f  height:%f.", image.size.width, image.size.height);
      
      [self.imgView setImage:image];
      
   }
   NSLog(@"previous btn press");
}

- (IBAction)btnDismiss:(id)sender
{
   NSLog(@"dismiss btn press");
   [self dismissViewControllerAnimated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
